package com.example.example;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class ListViewActivity extends AppCompatActivity {

    private String[] data = {"Apple", "Banana", "Orange", "Watermelon",
            "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango",
            "Apple", "Banana", "Orange", "Watermelon",
            "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango"};

    private List<Fruit> fruitList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);

        ArrayAdapter<String> adapter = new ArrayAdapter<>(
                ListViewActivity.this, android.R.layout.simple_list_item_1, data);
        ListView listView1 = findViewById(R.id.list_view1);
        listView1.setAdapter(adapter);

        initFruits();
        FruitAdapter fruitAdapter = new FruitAdapter(
                ListViewActivity.this, R.layout.fruit_item, fruitList);
        ListView listView2 = findViewById(R.id.list_view2);
        listView2.setAdapter(fruitAdapter);
        listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Fruit fruit = fruitList.get(position);
                Toast.makeText(ListViewActivity.this, fruit.getName(), Toast.LENGTH_LONG).show();
            }
        });
    }

    private void initFruits() {
        for (int i = 0; i < data.length; i++) {
            Fruit fruit = new Fruit(data[i], R.mipmap.ic_launcher_round);
            fruitList.add(fruit);
        }
    }

}
